{extend name="common/base" /}
{block name='title'}新增课程 - 课程管理{/block}

{block name="body"}
    <article class="Hui-admin-page-container">
        <form class="form form-horizontal" id="form-article-add">
            <div class="row clearfix">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>课程标题：</label>
                <div class="form-controls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" value="" placeholder="" id="title" name="title">
                </div>
            </div>
            <div class="row clearfix">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>简略标题：</label>
                <div class="form-controls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" value="" placeholder="" id="title_en" name="title_en">
                </div>
            </div>
            <div class="row clearfix">
                <label class="form-label col-xs-4 col-sm-2">所属班级：</label>
                <div class="form-controls col-xs-8 col-sm-9"> 
                    <span class="select-box" style="width:150px;">
                        <select class="select" name="class_id" size="1">
                            {foreach $classes as $k=>$v}
                            <option value="{$v.id}">{$v.title}</option>
                            {/foreach}
                        </select>
                    </span> 
                    <span class="select-box" style="width:150px;">
                        <select class="select" name="plan_id" size="1">
                            {foreach $plan as $k=>$v}
                            <option value="{$v.id}">{$v.title}</option>
                            {/foreach}
                        </select>
                    </span> 
                </div>
            </div>
            
            <div class="row clearfix">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>讲师：</label>
                <div class="form-controls col-xs-8 col-sm-9">
                    {foreach $lecturer as $k=>$v}
                    <input type="checkbox" value="{$v.id}" placeholder=""  name="lecturer">{$v.title}
                    {/foreach}
                </div>
            </div>
            <div class="row clearfix">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>排序值：</label>
                <div class="form-controls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" value="0" placeholder="" id="articleSort" name="sort">
                </div>
            </div>
            <div class="row clearfix">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>关键词：</label>
                <div class="form-controls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" value="" placeholder="" id="articleKeywords" name="key_words">
                </div>
            </div>
            <div class="row clearfix">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>课程摘要：</label>
                <div class="form-controls col-xs-8 col-sm-9">
                    <textarea cols="" rows="" class="textarea textarea-article"  placeholder="说点什么...最少输入10个字符" name="summary"></textarea>
                </div>
            </div>
            <div class="row clearfix">
                <label class="form-label col-xs-4 col-sm-2">评分：</label>
                <div class="form-controls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" value="5" placeholder="" id="score" name="score">
                </div>
            </div>
            <div class="row clearfix">
                <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>观看数量：</label>
                <div class="form-controls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" value="0" placeholder="" id="scan_num" name="scan_num">
                </div>
            </div>
            
            <div class="row clearfix">
                <label class="form-label col-xs-4 col-sm-2">试看：</label>
                <div class="form-controls col-xs-8 col-sm-9 skin-minimal">
                    <div class="radio-box">
                        <input name="is_try_see" type="radio" value="1" id="sex-1" >
                        <label for="sex-1">是</label>
                    </div>
                    <div class="radio-box">
                        <input type="radio" id="sex-2" value="-1" checked name="is_try_see">
                        <label for="sex-2">否</label>
                    </div>
                </div>
            </div>
            

            <div class="row clearfix">
                <label class="form-label col-xs-4 col-sm-2">封面图：</label>
                <div class="form-controls col-xs-8 col-sm-9">
                    <div id="uploadLists" ></div>
                    <span class="btn-upload">
                        <a href="javascript:void();" class="btn btn-primary radius btn-upload"><i class="Hui-iconfont">&#xe642;</i> 浏览文件</a>
                        <input type="file"  name="btn-upload" id="btn-upload" class="input-file">
                    </span>
                </div>
            </div>
            <div class="row clearfix">
                <label class="form-label col-xs-4 col-sm-2">课程详情：</label>
                <div class="form-controls col-xs-8 col-sm-9">
                    <script id="articleEditor" type="text/plain" style="width:100%;height:400px;"></script>
                </div>
            </div>
            <div class="row clearfix">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                    <button class="btn btn-primary radius" type="submit"><i class="Hui-iconfont">&#xe632;</i> 提交</button>
                    <button onClick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
                </div>
            </div>
        </form>
    </article>
    <input name="uploadImage" type="hidden" value="{:url('upload/uploadImage')}">
    <input name="image" type="hidden" >
    <input name="thumb" type="hidden" >
{/block}

{block name="js"}
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="__LIB__/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="__LIB__/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="__LIB__/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript" src="__LIB__/webuploader/0.1.5/webuploader.min.js"></script>
<script type="text/javascript" src="__LIB__/ueditor/1.4.3/ueditor.config.js"></script>
<script type="text/javascript" src="__LIB__/ueditor/1.4.3/ueditor.all.min.js"> </script>
<script type="text/javascript" src="__LIB__/ueditor/1.4.3/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" src="__JS__/main.js"></script>




<script type="text/javascript">
    $(function(){
        // 复选框样式
        $('.skin-minimal input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        });
        // 是否评论
        $('#articleComment').on('ifChecked', function(event){
            $("#comment-datetime-start,#comment-datetime-end").attr("disabled",false).removeClass("disabled");
        });
        $('#articleComment').on('ifUnchecked', function(event){
            $("#comment-datetime-start,#comment-datetime-end").attr("disabled",true).addClass("disabled");
        });

        // 设置评论日期
        $("#comment-datetime-start").datetimepicker({
        format: 'yyyy-mm-dd',
        minView: "month",
        todayBtn:  1,
        autoclose: 1,
        endDate : new Date(),
      }).on('changeDate', function(event) {
        event.preventDefault();
        event.stopPropagation();
        var startTime = event.date;
        $('#search-datetime-end').datetimepicker('setStartDate',startTime);
      });
      $("#comment-datetime-end").datetimepicker({
        format: 'yyyy-mm-dd',
        minView: "month",
        todayBtn:  1,
        autoclose: 1,
        endDate : new Date(),
      }).on('changeDate', function(event) {
        event.preventDefault();
        event.stopPropagation();
        var endTime = event.date;
        $("#search-datetime-start").datetimepicker('setEndDate',endTime);
      });

        // 课程摘要
        $(".textarea-article").Huitextarealength({
            minlength: 10,
            maxlength: 500
        });

        // 课程内容 实例化百度富文本编辑器
        var ue = UE.getEditor('articleEditor');
       
     
        // 表单提交验证
        $("#form-article-add").validate({
            rules: {
                title: {
                    required: true,
                },

            },
            onkeyup: false,
            focusCleanup: true,
            success: "valid",
            submitHandler: function(form){
                var url = "{:url('course/add')}";
                var title = $("input[name='title']").val();
                var plan_id = $("select[name='plan_id']").val();
                var title_en = $("input[name='title_en']").val();
                var key_words = $("input[name='key_words']").val();
                var score = $("input[name='score']").val();
                var scan_num = $("input[name='scan_num']").val();
                var image = $("input[name='image']").val();
                var thumb = $("input[name='thumb']").val();
                var summary = $("textarea[name='summary']").val();
                var sort = $("input[name='sort']").val();
                var is_try_see = $("input[name='is_try_see']:checked").val();
                var content = ue.getContent();
                
                var lecturer = [];
                $("input[name='lecturer']:checked").each(function(index,ele){
                    if($(ele).val() != 0){
                        lecturer.push($(ele).val());
                    }
                });
                
                var lecturers = lecturer.join(',');
                if(lecturers == ''){
                    layer.msg('请选择讲师',{icon:2,time:1000});
                    return false;
                }
                $.ajax({
                    url : url ,
                    type : 'post',
                    dataType : 'json',
                    data : {
                        title : title,
                        title_en : title_en,
                        key_words : key_words,
                        score : score,
                        scan_num : scan_num,
                        image : image,
                        thumb : thumb,
                        summary : summary,
                        sort : sort,
                        plan_id : plan_id,
                        lid : lecturers,
                        is_try_see : is_try_see,
                        content : content,
                    },
                    success :function(ret){
                        if(0 == ret.code){
                            layer.msg(ret.msg, { icon:1,time:1000 });
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.location.replace(parent.location.href)
                            parent.layer.close(index);
                        }else{
                            layer.msg(ret.msg, { icon:2,time:1000 });
                        }
                    }
                });
                //$(form).ajaxSubmit();
            }
        });
    });
    
    $("select[name='class_id']").bind('change', function(){
            var class_id = $(this).val();
            var url = "{:url('course/getPlan')}";
            $.ajax({
                url :url,
                type : 'post',
                dataType : 'json',
                data : {
                    class_id : class_id,
                },
                success :function(ret){
                    //console.log(ret);
                    if(0 == ret.code){
                        var  data = ret.data;
                        var str = '';
                        for(var i=0; i<data.length; i++){
                            str += '<option value="'+data[i]['id']+'">'+data[i]['title']+'</option>';
                        }
                        $("select[name='plan_id']").html(str);
                    }else{
                        layer.msg(ret.msg, { icon:2,time:1000 });
                    }
                }
            });
    });
</script>
<!--/请在上方写此页面业务相关的脚本-->
{/block}